/*
 * @Author: your name
 * @Date: 2021-02-09 13:59:44
 * @LastEditTime: 2021-02-10 16:55:36
 * @LastEditors: Please set LastEditors
 * @Description: 生成终端用户唯一标识
 * @FilePath: \h5-run-collector\src\collector\uuid.js
 */

import cookies from './helpers/cookies';

export default function() {
    const _uuid = cookies.read('dh5_uuid');
    if (_uuid) {
        return _uuid;
    }
    
    const s = [];
    const hexDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (let i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23] = "-";
    const uuid = s.join("");
    
    // 将uuid记录到cookit中，并将有效期设置成50年后
    const y = new Date().getFullYear() + 50; // 年份加50
    let expires = y + '/12/30';
    expires = new Date(expires).getTime();
    cookies.write('dh5_uuid', uuid, expires);

    return uuid;
}